home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 050 / madtrb10.arc / TREE.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1985-12-08  |  16.6 KB  |  230 lines

  1. Program MailOrder;
  2.  
  3. { *************************************************************************** }
  4. { *                                                                         * }
  5. { *                   QUEUE AND TREE THEORY PROGRAM                         * }
  6. { *                             Program #5                                  * }
  7. { *                                                                         * }
  8. { *                     Class: CS 367     Section: 4                        * }
  9. { *                         Instructor: Wiegand                             * }
  10. { *                                                                         * }
  11. { *                       Written by Chris Maeder                           * }
  12. { *                Edited and Compiled using Turbo Pascal                   * }
  13. { *                            on an IBM PC                                 * }
  14. { *                                                                         * }
  15. { *   DEFINITION:                                                           * }
  16. { *        A queue is a data structure which is used to get                 * }
  17. { *        first-in/first-out (FIFO) behavior.  Elements can be added to    * }
  18. { *        the queue but the only element that can be removed from the      * }
  19. { *        queue is the first-most element in the queue.                    * }
  20. { *                                                                         * }
  21. { *        Example queue:                                                   * }
  22. { *                                                                         * }
  23. { *         __     __     __     __     __     __     __                    * }
  24. { *        |__|-->|__|-->|__|-->|__|-->|__|-->|__|-->|__|-->Nil             * }
  25. { *                                                                         * }
  26. { *          ^--HeadPtr                                ^--TailPtr           * }
  27. { *                                                                         * }
  28. { *        This queue data structure can be represented in computer memory  * }
  29. { *        as a linked list of elements.  Note that the link between queue  * }
  30. { *        elements is implemented using pointers.  An ealier element       * }
  31. { *        points to the next element added to the queue.  A new element    * }
  32. { *        is inserted at the tail of the queue with the aid of a queue     * }
  33. { *        element tail pointer.  Note that the only element allowed to be  * }
  34. { *        removed from the queue is the element at the front of the        * }
  35. { *        queue.  A queue element head pointer points to the front         * }
  36. { *        element of the queue.  When the front element is removed from    * }
  37. { *        the queue its successor becomes the new front queue element.     * }
  38. { *                                                                         * }
  39. { *                                                                         * }
  40. { *        A binary tree is a tree data structure with one root node        * }
  41. { *        which has two binary trees as child nodes.  A binary search      * }
  42. { *        tree is a special binary tree whose nodes are organized in an    * }
  43. { *        ordered manner such that an inorder traversal of the tree        * }
  44. { *        gives the natural order of the elements.  For each node in a     * }
  45. { *        binary search tree, elements in its left subtree are before      * }
  46. { *        it (in natural order) and elements in its right subtree are      * }
  47. { *        after it (in natural order).                                     * }
  48. { *                                                                         * }
  49. { *        A binary search tree is very useful in storing data since it     * }
  50. { *        allows very fast access search speeds for a particular item.     * }
  51. { *        The principal reason for this this fast access speed is that     * }
  52. { *        at every node branch half of the tree data is removed from       * }
  53. { *        having to be looked at during a search.                          * }
  54. { *                                                                         * }
  55. { *        Sample data for the below example:                               * }
  56. { *                                                                         * }
  57. { *            Sally Tom George Ed Sam Frank Zena Don                       * }
  58. { *                                                                         * }
  59. { *        Example binary search tree whose nodes are ordered               * }
  60. { *        alphabetically after reading the above data:                     * }
  61. { *                                                                         * }
  62. { *                                Sally <--Root                            * }
  63. { *                               /     \                                   * }
  64. { *                         George       Tom                                * }
  65. { *                       / \              / \                              * }
  66. { *                     Ed   Nil        Sam   Zena                          * }
  67. { *                   / \             / \       / \                         * }
  68. { *                Don   Frank     Nil   Nil Nil   Nil                      * }
  69. { *              / \         / \                                            * }
  70. { *           Nil   Nil   Nil   Nil                                         * }
  71. { *                                                                         * }
  72. { *        The tree data structure can be represented in computer memory    * }
  73. { *        as tree node elements linked to the rest of the tree by either   * }
  74. { *        its parent's left child pointer or right child pointer.  Every   * }
  75. { *        binary search tree node element has a left node pointer          * }
  76. { *        pointing to a binary search subtree that is before it and a      * }
  77. { *        right node pointer pointing to a binary search subtree that is   * }
  78. { *        after it.                                                        * }
  79. { *                                                                         * }
  80. { *        It should be noted that new nodes are always inserted into a     * }
  81. { *        binary search tree as leaf nodes.                                * }
  82. { *                                                                         * }
  83. { *        Note that there are three special cases when deleting an         * }
  84. { *        element from a binary search tree:                               * }
  85. { *                                                                         * }
  86. { *              1. Delete an element with no children.                     * }
  87. { *              2. Delete an element with one child.                       * }
  88. { *              3. Delete an element with two children.                    * }
  89. { *                                                                         * }
  90. { *                                                                         * }
  91. { *   ALGORITHM:                                                            * }
  92. { *        Specific algorithms can be found with their respectful           * }
  93. { *        functions, procedures, and modules.                              * }
  94. { *                                                                         * }
  95. { *                                                                         * }
  96. { *   PURPOSE:                                                              * }
  97. { *        1. Print report heading.                                         * }
  98. { *        2. Process mail orders for a sporting goods store.               * }
  99. { *        3. The following data structures are to be used:                 * }
  100. { *           a. Use queues to hold transactions waiting to be processed.   * }
  101. { *           b. Use a circular doubly linked queue to maintain the         * }
  102. { *              employee list and current working employee.                * }
  103. { *           c. Use a binary search tree to hold the merchandise the       * }
  104. { *              store carries.                                             * }
  105. { *        4. All data structures are to be implemented using pointers.     * }
  106. { *        5. Employee commands are to be processed as they are read in     * }
  107. { *           from the date file.                                           * }
  108. { *           a. Hire an already existant clerk?  Ignore the command.       * }
  109. { *           b. An non-existant clerk quits?  Ignore the command.          * }
  110. { *           c. Otherwise process the command.                             * }
  111. { *        6. Each clerk does just two transactions.                        * }
  112. { *           a. A clerk that does one transaction at the end of the day    * }
  113. { *              does their second transaction at the start of the next     * }
  114. { *              day.                                                       * }
  115. { *        7. Transactions are to be processed one day at a time in the     * }
  116. { *           following order:                                              * }
  117. { *           a. New item transaction                                       * }
  118. { *              i.   Try to insert an already available item?  Treat it    * }
  119. { *                   as an add transaction.                                * }
  120. { *              ii.  Otherwise process the transaction.                    * }
  121. { *           b. Add a quantity to an item.                                 * }
  122. { *              i.   Try to add to a non-existant item?  Ignore the        * }
  123. { *                   transaction.                                          * }
  124. { *              ii.  Otherwise process the transaction.                    * }
  125. { *           c. Sell items                                                 * }
  126. { *              i.   Transactions processed first come, first serve.       * }
  127. { *              ii.  Try to sell a non-existant item?  Delay the           * }
  128. { *                   transaction until the next day.                       * }
  129. { *              iii. The item exists but the quantity is not sufficient?   * }
  130. { *                   Delay the transaction until the next day.             * }
  131. { *              iv.  Otherwise process the transaction.                    * }
  132. { *              v.   Note that a sell transaction may stay on the queue    * }
  133. { *                   the entire time.                                      * }
  134. { *           d. Remove items                                               * }
  135. { *              i.   Try to remove a non-existant item?  Ignore the        * }
  136. { *                   transaction.                                          * }
  137. { *              ii.  Otherwise process the transaction.                    * }
  138. { *                                                                         * }
  139. { *                                                                         * }
  140. { *   INPUT/OUTPUT:                                                         * }
  141. { *        1. Print program heading.                                        * }
  142. { *        2. Read the mail order transactions out of the data file.        * }
  143. { *        3. Output should be very clear and well spaced.                  * }
  144. { *        4. For each day the following output should be printed:          * }
  145. { *           a. Print each transaction as it is processed off of the       * }
  146. { *              queue.                                                     * }
  147. { *              i.   Print the type of transaction.                        * }
  148. { *              ii.  Print the mail order clerk that processed it.         * }
  149. { *           b. Print a list of current clerks.                            * }
  150. { *           c. Print a list of items left on the queues.                  * }
  151. { *        5. Print the inventory binary search tree out in alphabetical    * }
  152. { *           order.                                                        * }
  153. { *        6. Print the binary search tree out in tree form.                * }
  154. { *                                                                         * }
  155. { *                                                                         * }
  156. { *************************************************************************** }
  157.  
  158. Const
  159.   MAX_ENTRY_SIZE=25;                   { maximum size of entry string }
  160.   MAX_CLERK_NAME_SIZE=25;              { maximum size of clerk names }
  161.   MAX_ITEM_NAME_SIZE=20;               { maximum size of item names }
  162.   MAX_FILE_ENTRY_SIZE=55;              { maximum size of the fiel entry string }
  163.   FILE_NAME='Tree.Fil';
  164.   L_PRINT=False;                       { a boolean used to control the re-direction of the output to the printer }
  165.  
  166. Type
  167.   EntryString=String[MAX_ENTRY_SIZE];  { a string type used to store queue entries }
  168.   ClerkString=String[MAX_CLERK_NAME_SIZE]; { a string type used to store clerk names }
  169.   ItemString=String[MAX_ITEM_NAME_SIZE]; { a string type used to store item names }
  170.   FileEntryString=String[MAX_FILE_ENTRY_SIZE]; { a string type used to store file entries }
  171.  
  172.   QueueElementPtr=^QueueElementType;   { a pointer which points to the data record type 'QueueElementType' }
  173.   QueueElementType=                    { a doubly linked data record type used to store information in a queue }
  174.     Record
  175.       ItemName:EntryString;
  176.       ItemQuantity:EntryString;
  177.       Front:QueueElementPtr;           { a pointer to forward queue element }
  178.       Back:QueueElementPtr;            { a pointer to rearward queue element }
  179.     End; { QueueElementType }
  180.  
  181.   EmployeePtr=^EmployeeType;           { a pointer which points to the data record type 'QueueElementType' }
  182.   EmployeeType=                        { a doubly linked data record type used to store information in a queue }
  183.     Record
  184.       EmployeeName:EntryString;
  185.       ItemsProcessedDuringShift:Integer; { a counter used to keep track of how many items the employee
  186.                                            processed during his shift }
  187.       NextEmployee:EmployeePtr;        { a pointer to the next employee to take his shift }
  188.       LastEmployee:EmployeePtr;        { a pointer to the last employee to have his shift }
  189.     End; { EmployeeType }
  190.  
  191.   TreeNodePtr=^TreeNodeElementType;    { a pointer which points to the data record type 'TreeNodeElementType' }
  192.   TreeNodeElementType=                 { a binary tree node data type }
  193.     Record
  194.       ItemName:EntryString;
  195.       ItemQuantity:Integer;
  196.       Left:TreeNodePtr;                { a pointer to left tree node on next lower level }
  197.       Right:TreeNodePtr;               { a pointer to right tree node on next lower level }
  198.     End; { TreeNodeElementType }
  199.  
  200.  
  201. Var
  202.   NewItemHeadPtr,NewItemTailPtr:QueueElementPtr;           { pointers to the front and rear of the queue used to store
  203.                                                              new items to be added to the store's inventory }
  204.   AddQuantityHeadPtr,AddQuantityTailPtr:QueueElementPtr;   { pointers to the front and rear of the queue used to store
  205.                                                              item stock shipments }
  206.   SellQuantityHeadPtr,SellQuantityTailPtr:QueueElementPtr; { pointers to the front and rear of the queue used to store
  207.                                                              items sold from the store's stock }
  208.   RemoveItemHeadPtr,RemoveItemTailPtr:QueueElementPtr;     { pointers to the front and rear of the queue used to store
  209.                                                              items to be removed from the store's inventory }
  210.   CurrentEmployeePtr:EmployeePtr;                          { a pointer which points to the current employee that is
  211.                                                              on his work shift }
  212.   HoldPtr:Integer;                                         { a temporary pointer used in the re-directing of output
  213.                                                              from the screen to the printer }
  214.   TreeRootPtr:TreeNodePtr;                                 { a pointer which points to the root of the binary search
  215.                                                              tree which stores the store's inventory }
  216.  
  217. {$I Tree.Inc} { compliler include directive to include the file 'Tree.Inc' }
  218.  
  219.  
  220. Begin   { Main Program }
  221.   HoldPtr:=ConOutPtr;                  { temporarily store current output device address }
  222.   If L_PRINT Then
  223.     ConOutPtr:=LstOutPtr;              { re-direct output to the printer }
  224.   PrintHeading;
  225.   InitPointers;
  226.   ReadInputFile;
  227.   PrintInventoryModule;
  228.   PrintBinaryTree;
  229.   ConOutPtr:=HoldPtr;                  { reset output device address }
  230. End.    { Main Program }